import { defineStore } from 'pinia';

export const realTimeStore = defineStore('realTime', {
  state: () => ({
    UserName: "",
    currentDate: '',
    currenttime: '',
    week: '',
    // 浏览器是否在全屏状态下
    isFullscreen: false,
    // 检测是否是移动端
    mobile: false,
    id: ''


  }),
  actions: {
    getTime() {
      if (this.id) clearInterval(this.id)
      this.id = setInterval(() => {
        const now = new Date();
        const year = now.getFullYear();
        const month = String(now.getMonth() + 1).padStart(2, "0");
        const day = String(now.getDate()).padStart(2, "0");
        const hours = String(now.getHours()).padStart(2, "0");
        const minutes = String(now.getMinutes()).padStart(2, "0");
        const seconds = String(now.getSeconds()).padStart(2, "0");
        const days = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
        this.currentDate = `${year}/${month}/${day}`
        this.currenttime = `${hours}:${minutes}:${seconds}`
        this.week = days[new Date().getDay()]
      }, 1000)
    },
    clearId() {
      if (this.id) clearInterval(this.id)
    },
    isMobile() {
      if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {
        this.mobile = true
      } else {
        //电脑
        this.mobile = false
      }
    }
  },
  persist: true,

});

